import {createRouter, createWebHistory} from 'vue-router'
import {routes} from "@/router/routes.ts";
import {useUserStore} from "@/store";
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'

const router = createRouter({
    history: createWebHistory('/'),
    routes,
});

// 路由守卫
router.beforeEach((to) => {
    nprogress.start()
    // 如果用户已经登录，或者用户访问的是登录页面，就放行
    if (!useUserStore().userState.token && to.path !== '/login') return '/login'
})

router.afterEach(() => {
    nprogress.done()
})

export {
    router
}
